package com.eras.shoes.view

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.eras.shoes.R


class DialogScanCode : Dialog {

    constructor(context: Context) : super(context) {}
    constructor(context: Context, theme: Int) : super(context, theme) {}

    class Builder(context: Context) {
        private var contentView: View? = null
        private var positiveButtonText: String? = null
        private var negativeButtonText: String? = null
        private var positiveButtonClickListener: View.OnClickListener? = null
        private var negativeButtonClickListener: View.OnClickListener? = null
        private val layout: View
        private val dialog: DialogScanCode = DialogScanCode(context, R.style.ScanCodeDialog)
        init {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            layout = inflater.inflate(R.layout.dialog_scan_code, null)
            dialog.addContentView(layout, null)
        }

        fun setContentView(v: View): Builder {
            this.contentView = v
            return this
        }

        fun setPositiveButton(positiveButtonText: String, listener: View.OnClickListener): Builder {
            this.positiveButtonText = positiveButtonText
            this.positiveButtonClickListener = listener
            return this
        }

        fun setNegativeButton(negativeButtonText: String, listener: View.OnClickListener): Builder {
            this.negativeButtonText = negativeButtonText
            this.negativeButtonClickListener = listener
            return this
        }

        /**
         * 创建双按钮对话框
         * @return
         */
        fun createButtonDialog(): DialogScanCode {
            showButton()
            layout.findViewById<View>(R.id.dialog_tv_cancel).setOnClickListener(positiveButtonClickListener)
            layout.findViewById<View>(R.id.dialog_tv_ture).setOnClickListener(negativeButtonClickListener)
            create()
            return dialog
        }
        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private fun create() {
            layout.findViewById<View>(R.id.sv_scan_code)
            dialog.setContentView(layout)
            dialog.setCancelable(true)     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false)        //用户不能通过点击对话框之外的地方取消对话框显示

        }
        /**
         * 显示双按钮布局，隐藏单按钮
         */
        private fun showButton() {
            layout.findViewById<View>(R.id.dialog_tv_cancel).visibility = View.VISIBLE
            layout.findViewById<View>(R.id.dialog_tv_ture).visibility = View.VISIBLE
            layout.findViewById<View>(R.id.dialog_tv_reset).visibility = View.VISIBLE
        }

    }

}